package DesignPatern.A16_观察者模式.QSB.A01_基础模式;

import java.util.ArrayList;

/**
 * @Author: Du
 * @Date: 2021/8/2 11:53
 *
 * 被观察者
 */
public class HanFeiZi implements Observable,IHanFeiZi
{
    private ArrayList<Observer> observers = new ArrayList<>();
    
    @Override
    public void addObserver(Observer observer)
    {
        this.observers.add(observer);
    }
    
    @Override
    public void deleteObserver(Observer observer)
    {
        this.observers.remove(observer);
    }
    
    @Override
    public void notifyObservers(String context)
    {
        for (Observer o : observers)
        {
           o.update(context);
        }
    }
    
    @Override
    public void haveBreakFast()
    {
        System.out.println("韩非子要吃饭");
        this.notifyObservers("韩非子在吃饭");
    }
    
    @Override
    public void haveFun()
    {
        System.out.println("韩非子要休息");
        this.notifyObservers("韩非子在休息");
    }
}
